//
//  TimePickerView.m
//  选择时间
//
//  Created by LeeBruce on 16/12/2.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "TimePickerView.h"

@interface TimePickerView() <UIPickerViewDelegate>


@end

@implementation TimePickerView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        [self initSubviews];
        
    }

    return self;
}


-(void)initSubviews{
    
    UIView *dateView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
    dateView.backgroundColor = GrayColor;
    
    self.datePicker = [[UIDatePicker alloc]init];
    self.datePicker.backgroundColor = [UIColor whiteColor];
    self.datePicker.frame = CGRectMake(0, 40, screenWidth, 200);
    [dateView addSubview:self.datePicker];
    [self.datePicker setDate:[NSDate date] animated:YES];
    
    /*
    *  设置只显示中文
    */
        [self.datePicker setLocale:[NSLocale localeWithLocaleIdentifier:@"zh-CN"]];
    
    self.datePicker.timeZone = [NSTimeZone timeZoneWithName:@"GTM+8"];
    self.datePicker.datePickerMode = UIDatePickerModeDate; // 设置样式
    [self.datePicker addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:dateView];
    
    //初始化timestring
    NSDateFormatter *forma = [[NSDateFormatter alloc]init];
    
    [forma setDateFormat:@"YYYY-MM-dd"];
//   self.timeString = [forma stringFromDate: [NSDate date]]; //UIDatePicker显示的时间
    self.timeString = self.value;
    
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setTitle:@"取消" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    leftButton.frame = CGRectMake(18, 0, 70, 40);
    [dateView addSubview:leftButton];
    [leftButton addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"确定" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    rightButton.frame = CGRectMake(self.frame.size.width - 88, 0, 70, 40);
    [dateView addSubview:rightButton];
    [rightButton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];


}

-(void)leftButtonAction
{
    if (self.callBack) {
        self.callBack(nil);
    }

    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.alpha = 0.3;
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];

    }];
    
}

-(void)changeValue:(UIDatePicker *)picker
{
    NSDateFormatter *forma = [[NSDateFormatter alloc]init];
    
    [forma setDateFormat:@"YYYY-MM-dd"];

    NSString *str = [forma stringFromDate:picker.date]; //UIDatePicker显示的时间
    
    self.timeString = str;
    
}

-(void)rightButtonAction
{
    
    if (self.callBack) {
        self.callBack(self.timeString);
    }

    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.alpha = 0.3;
    } completion:^(BOOL finished) {
        
        
        [self removeFromSuperview];

        
    }];}


@end
